package com.example.customview.recyclerview;

import androidx.annotation.ContentView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.customview.R;

import java.util.ArrayList;
import java.util.List;

public class BaseUseActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    private List<String> mDates;
    private BaseRecyclerViewAdapter mBaseRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_use);
        initData();
        initView();
    }

    private void initView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        //指定显示样式
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBaseRecyclerViewAdapter = new BaseRecyclerViewAdapter(this,mDates);
        mRecyclerView.setAdapter(mBaseRecyclerViewAdapter);
        //添加分割线
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration());
    }

    protected void initData() {
        mDates = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++) {
            mDates.add("" + (char) i);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //菜单选项
        switch (item.getItemId()) {
            case R.id.id_action_gridview:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                break;
            case R.id.id_action_listview:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                break;
        }
        return true;
    }

    class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseRecyclerViewAdapter.MyViewHolder>{
         private List<String> mDates;
         private LayoutInflater mInflater;

         public BaseRecyclerViewAdapter(Context context,List<String> dates) {
             mInflater = LayoutInflater.from(context);
             mDates = dates;
         }

         @NonNull
         @Override
         public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
             MyViewHolder myViewHoder = new MyViewHolder(mInflater.
                     inflate(R.layout.item_home,parent,false));
             return myViewHoder;
         }

         @Override
         public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
             Log.i("onBindViewHolder",mDates.get(position));
             holder.mItemTextView.setText(mDates.get(position));
         }

         @Override
         public int getItemCount() {
             return mDates.size();
         }

         class MyViewHolder extends RecyclerView.ViewHolder{
             private TextView mItemTextView;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                mItemTextView = itemView.findViewById(R.id.id_num);
            }
        }
    }
}